<?php
namespace app\common\bonus;

Class Lingshou extends Common
{
	static $bonus_name='lingshou';

	public function handle($data){
		$actionData = $data['action_data'];

		$reason_time = $data['create_time'];
		$reason_day = date('Ymd',$reason_time);

		$uid = $actionData['uid'];
		$score = $actionData['score'];
		$level_id = $actionData['level_id'];

		// 获取本插件配置数据
		$bonusConfigModel = model('bonus_config');
		$model = model('bonus_config');
		// 货币配置
		$coinConfig = $bonusConfigModel::bonusConfig(self::$bonus_name,'coin');
		$coinConfig = unserialize($coinConfig['config_value']);
		// 推荐人配置
		$inviterConfig = $bonusConfigModel::bonusConfig(self::$bonus_name,'inviter');
		$inviterConfig = unserialize($inviterConfig['config_value']);

		$model->startTrans();
		try{
			foreach($inviterConfig as $key => $config):
				// 该会员等级在第$key层级推荐人的配置
				$rate = $config[$level_id]['rate'];
				$max = $config[$level_id]['max'];
				// 该层级的推荐人id
				$inviter_id = model('member_inviter')->getInviter($uid,$key);
				if(false==$inviter_id){
					// 如果该推荐层级没有推荐人，终止处理
					break;
				}
				$bonus = $score * $rate / 100;

				// 累计奖金
				$this->incTotal($bonus,$inviter_id,$reason_day,self::$bonus_name);
				// 货币分配
				$this->bonus_record($coinConfig,self::$bonus_name,$uid,$inviter_id,$bonus,$reason_time);
			endforeach;

			$model->commit();
		}catch(\Exception $e){
			$model->rollback();
			$this->error = $e->getMessage();
			return false;
		}
		return true;
	}

}